<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<link rel='stylesheet' href='../css/style.css' />
		<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../themes/icon.css">
		<script type="text/javascript" src="../js/xheditor-1.2.2/jquery/jquery-1.11.2.min.js"></script>
	    <script type="text/javascript" src="../js/xheditor-1.2.2/xheditor-1.2.2.min.js"></script>
	    <script type="text/javascript" src="../js/xheditor-1.2.2/xheditor_lang/zh-cn.js"></script>
  		<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
		<title></title>
	</head>
	<body>
		
  	<table id="dg" title="评论列表" class="easyui-datagrid" toolbar="#toolbar" pagination="true"
            rownumbers="true" fitColumns="true" singleSelect="true" fit="true"  >
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newComment()">添加评论</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editComment()">修改评论</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyComment()">删除评论r</a>
    </div>
    
    <div id="dlg" class="easyui-dialog" style="width:400px"
            closed="true" buttons="#dlg-buttons">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <textarea  name="comment" id="content" cols="120" rows="6" style="width:100%;height:100%"></textarea>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveComment()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
    </div>
    <script type="text/javascript">
        var url,method;
        initComment();
        function initComment(){
	        $('#dg').datagrid({
		        fit:true,
		        border:false,
		        pagination:true,
		        pageSize:5,
		        pageList: [5,10,15,20],
		        url:'http://localhost:3000/comments/list',
		        columns:[[
		            {field:'comment',title:'评论',width:100},
		            {field:'newsid',title:'新闻',width:100,align:'right'}
		        ]]
	    	});
    	}

        function newComment(){
            $('#dlg').dialog('open').dialog('center').dialog('setTitle','添加评论');
            $('#fm').form('clear');
            url = 'http://localhost:3000/comments/data';
            method = 'post';
        }
        function editComment(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('center').dialog('setTitle','修改评论');
                $('#fm').form('load',row);
                url = 'http://localhost:3000/comments/data/'+row._id;
                method = 'put';
            }
        }
        function saveComment(){
            $('#fm').form('submit',{
                url: url,
                method:method,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    if (result.errorMsg){
                        $.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    } else {
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the user data
                    }
                }
            });
        }
        function destroyComment(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                url = 'http://localhost:3000/comments/data/'+row._id;
                $.messager.confirm('Confirm','确定要删除该评论吗?',function(r){
                    if (r){
                        $.ajax({
                            url: url,
                            method:"delete"
                            }).done(function(result) {
                                if(result.message){
                                    $('#dg').datagrid('reload');
                                }
                            });
                    }
                });
            }
        }
		
		// $(pageInit);
	    // function pageInit()
	    // {
	    //     $.extend(XHEDITOR.settings,{shortcuts:{'ctrl+enter':submitForm}});
	    //     $('#content').xheditor(
	    //         {upLinkUrl:"upload.php",
	    //         upLinkExt:"zip,rar,txt",
	
	    //         upImgUrl:"/comment/uploadImg",
	    //         upImgExt:"jpg,jpeg,gif,png",
	
	    //         upFlashUrl:"upload.php",
	    //         upFlashExt:"swf",
	            
	    //         upMediaUrl:"upload.php",
	    //         upMediaExt:"wmv,avi,wma,mp3,mid"});
	    // }
	    // function insertUpload(arrMsg)
	    // {console.log(arrMsg)
	    //     var i,msg;
	    //     for(i=0;i<arrMsg.length;i++)
	    //     {
	    //         msg=arrMsg[i];
	    //         $("#uploadList").append('<option value="'+msg.id+'">'+msg.localname+'</option>');
	    //     }
	    // }
	    // function submitForm(){$('#frmDemo').submit()};
		</script>
		
	</body>
</html>
